<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Security</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<button onclick="postTest()">点我发送Post请求</button>
<br />
<button onclick="corsApiTest()">点我测试Cors跨域接口请求</button>
<script>
    const postTest = () => {
        axios.post('http://localhost:8888/user/login', {
            userName: 'feng',
            password: '1234'
        })
            .then(function (response) {
                console.log(response);
                //在这里将服务器返回的token存入cookie中
                localStorage.setItem('TOKEN',response.data.data.token)
            })
            .catch(function (error) {
                console.log(error);
            })
    }

    const corsApiTest = () => {
        axios.get('http://localhost:8888/testCors',
            //设置axios请求头：加上token字段
            {
            headers:{
                "token": localStorage.getItem('TOKEN')
            }
        })
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            })
    }
</script>
</body>
</html>